package org.xlin.biz.controller.rest.table;

import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.xlin.biz.service.table.TableService;
import org.xlin.biz.util.ResponseUtil;

/**
 * 
 * 桌位信息异步请求处理类
 * @author xl
 *
 */
@RestController
@RequestMapping("/table")
public class TableController {
	
	Logger logger = LoggerFactory.getLogger(TableController.class);
	
	@Resource
	TableService tableService;
	
	/**
	 * 分页查询桌位列表
	 * @param pageable
	 * @param condition
	 * @return
	 */
    @RequestMapping(value="/list",method = RequestMethod.POST)
    public ResponseEntity<?> queryRecord4Page(@PageableDefault Pageable pageable,
			@RequestParam Map<String,Object> condition){
		logger.debug("分页查询桌位列表：{}",condition);
		try {
			return ResponseUtil.json(tableService.queryRecord4Page(pageable, condition), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("分页查询桌位列表失败：{}",e);
			return ResponseUtil.text("查询失败！",HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
    
    /**
	 * 增加桌位
	 * @param condition
	 * @return
	 */
    @RequestMapping(value="/oper",method = RequestMethod.POST)
	public ResponseEntity<?> addRecord(@RequestBody Map<String,Object> condition){
		logger.debug("增加桌位：{}",condition);
		try {
			tableService.addRecord(condition);
			return ResponseUtil.text("保存成功！",HttpStatus.OK);
		} catch (Exception e) {
			logger.error("增加桌位失败：{}",e);
			return ResponseUtil.text("保存失败！",HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	/**
	 * 更新桌位
	 * @param condition
	 * @return
	 */
    @RequestMapping(value="/oper",method = RequestMethod.PUT)
	public ResponseEntity<?> modfiyRecord(@RequestBody Map<String,Object> condition){
		logger.debug("更新桌位：{}",condition);
		try {
			int count=tableService.modfiyRecord(condition);
			if(count>0){
				return ResponseUtil.text("更新成功！", HttpStatus.OK);
			}
			return ResponseUtil.text("更新失败，记录不存在。", HttpStatus.INTERNAL_SERVER_ERROR);
		} catch (Exception e) {
			logger.error("更新桌位失败：{}",e);
			return ResponseUtil.text("更新失败！",HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	/**
	 * 删除桌位
	 * @param condition
	 * @return
	 */
    @RequestMapping(value="/oper",method = RequestMethod.DELETE)
	public ResponseEntity<?> deleteRecord(@RequestParam Map<String,Object> condition){
		logger.debug("删除桌位：{}",condition);
		try {
			tableService.deleteRecord(condition);
			return ResponseUtil.text("删除成功！",HttpStatus.OK);
		} catch (Exception e) {
			logger.error("删除桌位失败：{}",e);
			return ResponseUtil.text("删除失败！",HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

}
